package com.weng.platform.platform.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.platform.entity.Project;
import com.weng.platform.platform.model.vo.ProjectVo;
import com.weng.platform.platform.service.IProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;


/**
 * 平台项目 API实现
 *
 * @author weng
 * @date 2021-01-15 17:18:40
 *
 */  
@Slf4j
@RestController
@RequestMapping("/projects")
@Api(value = "平台项目api", tags = {"平台项目操作接口"})
public class ProjectApi extends BaseController {
    @Autowired
    private IProjectService projectService;

    /**
     * 功能描述:单个获取平台项目
     */
    @ApiOperation(value = "获取平台项目", notes = "")
    @GetMapping(value = "/{id}")
    public ResponseData get(@PathVariable("id") String id){
        Project project = projectService.getById(id);
        return success(project);
    }


    /**
     * 条件 分页查询平台项目
     */
    @ApiOperation(value = "分页获取平台项目", notes = "")
    @GetMapping(value = "")
    public ResponseData list(Integer pageNo,Integer pageSize, HttpServletRequest request, String name, String type){
				String ownerId = CloudAuthUtil.getCurUserId(request);
				IPage<Project> iPage = projectService.list(pageNo, pageSize, ownerId, name, type);
        return success(iPage);
    }


    /**
	 * 新增平台项目
	 */
	@ApiOperation(value = "新增平台项目", notes = "")
	@PostMapping(value = "")
	public ResponseData add(@Valid @RequestBody ProjectVo projectReq, HttpServletRequest request){
		Project project = Convert.convert(Project.class, projectReq);
		String userId = CloudAuthUtil.getCurUserId(request);
		String userName = CloudAuthUtil.getCurUserTrueName(request);
		project.setOwnerId(userId);
		project.setOwnerName(userName);
		projectService.add(project);
		return success();
	}

	/**
	 * 更新平台项目
	 */
	@ApiOperation(value = "更新平台项目", notes = "")
	@PutMapping(value = "")
	public ResponseData update(@Valid @RequestBody ProjectVo projectReq){
		Project project = Convert.convert(Project.class, projectReq);
		projectService.update(project);
		return success();
	}


	/**
	 * 删除平台项目
	 */
	@ApiOperation(value = "ids 删除平台项目", notes = "可以删除多个，中间以,号分隔")
	@DeleteMapping(value = "")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		projectService.removeByIds(Arrays.asList(ids.split(",")));
		return success(deleteReq.getIds());
	}
}
